const format = require("../../../utils/format.js")
const app = getApp()
let formIdArr = []
Page({
    data: {
        isPop: false,
        loading: false,
        time: '',
        multiIndex: [0, 0, 0, 0, 0],
        multiArray: [],
        year: "",
        month: "",
        day: "",
    },
    //月份计算
    surplusMonth: function(year) {
        var year2 = this.getDate().getFullYear()
        var month = this.getDate().getMonth() + 1
        var day = this.getDate().getDate()
        var hour = this.getDate().getHours()
        var minute = this.getDate().getMinutes()
        var second = this.getDate().getSeconds()
        var monthDatas = [];
        if (year == year2) {
            var surplusMonth = 12 - month;
            monthDatas.push(this.withData(month) + "月")
            for (var i = month; i < 12; i++) {
                monthDatas.push(this.withData(parseInt(i + 1)) + "月")
            }
        } else {
            for (var i = 0; i < 12; i++) {
                monthDatas.push(this.withData(parseInt(i + 1)) + "月")
            }
        }

        return monthDatas;
    },
    //天数计算
    surplusDay: function(year, month, day) {
        var days = 31;
        var dayDatas = [];
        var year2 = this.getDate().getFullYear()
        var month2 = this.getDate().getMonth() + 1

        switch (parseInt(month)) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
                //对于2月份需要判断是否为闰年
            case 2:
                if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
                    days = 29;
                    break;
                } else {
                    days = 28;
                    break;
                }

            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
        }
        if (year == year2 && month == month2) {
            dayDatas.push(this.withData(day) + "日")
            for (var i = day; i < days; i++) {
                dayDatas.push(this.withData(parseInt(i + 1)) + "日")
            }
        } else {
            console.log(month + "月" + days + "天")
            for (var i = 0; i < days; i++) {
                dayDatas.push(this.withData(parseInt(i + 1)) + "日")
            }
        }
        return dayDatas;
    },
    //时间计算
    surplusHour: function(year, month, day, hour) {
        console.log(hour, 'ceshihour')
        var hours = 23;
        var year2 = this.getDate().getFullYear()
        var month2 = this.getDate().getMonth() + 1
        var day2 = this.getDate().getDate();
        var hourDatas = []
        var surplusHours = []

        surplusHours.push(this.withData(hour) + "时")
        if (year == year2 && month == month2 && day == day2) {
            for (var i = hour; i < hours; i++) {
                surplusHours.push(this.withData(parseInt(i + 1)) + "时")
            }
        } else {
            for (var i = 0; i < hours; i++) {
                surplusHours.push(this.withData(parseInt(i + 1)) + "时")
            }
        }
        hours = surplusHours
        return hours;
    },
    //分钟计算
    surplusMinute: function(year, month, day, hour, minute) {
        var minutes = 59;
        var year2 = this.getDate().getFullYear()
        var month2 = this.getDate().getMonth() + 1
        var day2 = this.getDate().getDate();
        var hour2 = this.getDate().getHours();
        var minuteDatas = []
        var surplusMinute = []
        console.log(month, day, hour, 'xxxxx')

        surplusMinute.push(this.withData(minute) + "分")
        if (year == year2 && month == month2 && day == day2 && hour == hour2) {
            console.log(month, day, hour, 'yes')
            for (var i = minute; i < minutes; i++) {
                surplusMinute.push(this.withData(parseInt(i + 1)) + "分")
            }
        } else {
            for (var i = 0; i < minutes; i++) {
                surplusMinute.push(this.withData(parseInt(i + 1)) + "分")
            }
        }
        return surplusMinute;
    },
    withData(param) {
        return param < 10 ? '0' + param : '' + param;
    },
    getDate() {
		var close_time_set = wx.getStorageSync('TICKET_ENDTIME_LIMIT')
        var this_time = new Date().getTime()
        var date = new Date(this_time + parseInt(close_time_set) * 24 * 60 * 60 * 1000 + 2 * 60 * 1000);
        return date
    },
    onLoad(options) {

        var year = this.getDate().getFullYear()
        var month = this.getDate().getMonth() + 1
        var day = this.getDate().getDate()
        var hour = this.getDate().getHours()
        var minute = this.getDate().getMinutes()

        var surplusMonth = this.surplusMonth(year);
        var surplusDay = this.surplusDay(year, month, day);
        var surplusHour = this.surplusHour(year, month, day, hour)
        var surplusMinute = this.surplusMinute(year, month, day, hour, minute)

        this.setData({
            carObj: options,
            multiArray: [
                [year + '年', (year + 1) + '年', (year + 2) + '年'],
                surplusMonth,
                surplusDay,
                surplusHour,
                surplusMinute,

            ],
            year: year,
            month: month,
            day: day,
            hour: hour,
            minute: minute,
        })
        this.loadData()
    },
    loadData() {
        if (this.data.loading) return
        wx.showLoading({
            title: '加载中...',
        })
        this.setData({
            loading: true
        })
        app.http.post('rest/violation/getViolations.do', {
            data: this.data.carObj
        }).then(res => {
            this.setData({
                list: res.obj.map(o => {
                    o.checked = false
                    return o
                }),
                loading: false
            })
            wx.hideLoading()
        }).catch(err => {
            wx.hideLoading()
            this.setData({
                loading: false
            })
            app.wxToast.error(err.data.msg)
        })
    },
    bindinput(evt) {
        let key = evt.currentTarget.dataset.key
        this.setData({
            [`${key}`]: evt.detail.value
        })
    },
    /**
     * 多项选择
     */
    checkboxChange: function(e) {
        let list = this.data.list
        let selectedList = []
        let values = e.detail.value
        for (let i = 0; i < list.length; ++i) {
            list[i].checked = false;
            for (let j = 0; j < values.length; ++j) {
                if (list[i].code == values[j]) {
                    list[i].checked = true;
                    selectedList.push(list[i])
                    break;
                }
            }
        }
        this.setData({
            list,
            selectedList
        });
        this.totalCount()
    },
    /**
     * 计算总数
     */
    totalCount: function() {
        let selectedList = this.data.selectedList
        let totalMoney = 0
        let totalPoint = 0
        if (selectedList.length == 0) {
            return
        }
        selectedList.forEach(o => {
            totalMoney += o.fine
            totalPoint += o.point
        })
        this.setData({
            totalMoney,
            totalPoint,
        })
    },
    //发布代缴按钮
    issueBtn() {
        let {
            selectedList = []
        } = this.data
        if (selectedList.length == 0) {
            wx.showToast({
                icon: 'none',
                title: '您还未选择任何代办项目哦~',
            })
            return
        }
        this.setData({
            isPop: true
        })
    },
    checkPopGency() {
        this.setData({
            isPop: !this.data.isPop
        })
    },
    //发布submit
    submit() {
        let {
            carObj = {}, selectedList = [], remark = '', time = '',
        } = this.data
        let codeArr = selectedList.map(o => o.code)
        if (!time) {
            app.wxToast.warn('请选择截止时间')
            return
        }
        wx.showLoading({
            title: '请稍等...',
        })

        app.http.post('rest/ticket/save.do', {
            data: {
                carNo: carObj.carNo,
                violationCodes: codeArr.join(','),
                endTime: time,
                remark,
            }
        }).then(res => {
            wx.hideLoading()
            this.checkPopGency()
            res.obj && wx.navigateTo({
                url: '/pages/car-owner/peccancy/waite-offer?id=' + res.obj,
            })
        }).catch(err => {
            wx.hideLoading()
            app.wxToast.error(err.msg)
        })
    },
    //某一列的值改变时触发
    bindMultiPickerColumnChange: function(e) {
        var year1 = this.getDate().getFullYear()
        var month1 = this.getDate().getMonth() + 1
        var day1 = this.getDate().getDate()
        var hour1 = this.getDate().getHours()
        console.log("当前年份" + this.data.month + '修改的列为', e.detail.column, '，值为', e.detail.value);
        var data = {
            multiArray: this.data.multiArray,
            multiIndex: this.data.multiIndex,
            year: this.data.year,
            month: this.data.month,
            day: this.data.day,
            hour: this.data.hour,
            minute: this.data.minute,
        };
        data.multiIndex[e.detail.column] = e.detail.value;
        switch (e.detail.column) {
            case 0:
                var yearStr = data.multiArray[e.detail.column][e.detail.value];
                var year = yearStr.substring(0, yearStr.length - 1)
                data.year = parseInt(year);
                var surplusMonth = this.surplusMonth(year);
                data.multiArray[1] = surplusMonth;

                if (data.year == year1) {
                    data.month = month1;
                } else {
                    data.month = 1;
                }
                if (data.year == year1 && month1 == data.month) {
                    data.day = day1;
                } else {
                    data.day = 1;
                }
                var surplusDay = this.surplusDay(data.year, data.month, data.day);
                data.multiArray[2] = surplusDay;


                var surplusHour;
                if (data.year == year1 && month1 == data.month && data.day == day1) {
                    surplusHour = this.surplusHour(data.year, data.month, data.day, data.hour)

                } else {
                    surplusHour = this.surplusHour(data.year, data.month, data.day, 0)

                }
                data.multiArray[3] = surplusHour;


                var surplusMinute;
                if (data.year == year1 && month1 == data.month && data.day == day1 && data.hour == hour1) {
                    surplusMinute = this.surplusMinute(data.year, data.month, data.day, data.hour, data.minute)

                } else {
                    surplusMinute = this.surplusMinute(data.year, data.month, data.day, data.hour, 0)

                }
                data.multiArray[4] = surplusMinute;

                data.multiIndex[1] = 0;
                data.multiIndex[2] = 0;
                data.multiIndex[3] = 0;
                data.multiIndex[4] = 0;
                break;
            case 1:
                console.log('选择月份' + data.multiArray[e.detail.column][e.detail.value]);

                var monthStr = data.multiArray[e.detail.column][e.detail.value];
                var month = monthStr.substring(0, monthStr.length - 1);

                data.month = month;
                data.day = 1;

                if (data.year == year1 && month1 == data.month) {
                    data.day = day1;
                } else {
                    data.day = 1;
                }
                var surplusDay = this.surplusDay(data.year, data.month, data.day);
                data.multiArray[2] = surplusDay;

                var surplusHour;
                if (data.year == year1 && month1 == data.month && data.day == day1) {
                    surplusHour = this.surplusHour(data.year, data.month, data.day, data.hour)
                } else {
                    surplusHour = this.surplusHour(data.year, data.month, data.day, 0)
                }
                data.multiArray[3] = surplusHour;

                var surplusMinute;
                if (data.year == year1 && month1 == data.month && data.day == day1 && data.hour == hour1) {
                    surplusMinute = this.surplusMinute(data.year, data.month, data.day, data.hour, data.minute)

                } else {
                    surplusMinute = this.surplusMinute(data.year, data.month, data.day, data.hour, 0)

                }
                data.multiArray[4] = surplusMinute;


                data.multiIndex[2] = 0;
                data.multiIndex[3] = 0;
                data.multiIndex[4] = 0;
                break;
            case 2:
                console.log('选择日' + data.multiArray[e.detail.column][e.detail.value]);
                var dayStr = data.multiArray[e.detail.column][e.detail.value];
                var day = dayStr.substring(0, dayStr.length - 1);
                data.day = day;

                var surplusHour;
                if (data.year == year1 && month1 == data.month && data.day == day1) {
                    surplusHour = this.surplusHour(data.year, data.month, data.day, data.hour)
                } else {
                    surplusHour = this.surplusHour(data.year, data.month, data.day, 0)
                }
                data.multiArray[3] = surplusHour;

                var surplusMinute;
                if (data.year == year1 && month1 == data.month && data.day == day1 && data.hour == hour1) {
                    surplusMinute = this.surplusMinute(data.year, data.month, data.day, data.hour, data.minute)

                } else {
                    surplusMinute = this.surplusMinute(data.year, data.month, data.day, data.hour, 0)

                }
                data.multiArray[4] = surplusMinute;

                data.multiIndex[3] = 0;
                data.multiIndex[4] = 0;
                break;
            case 3:
                console.log('选择时' + data.multiArray[e.detail.column][e.detail.value]);
                var hourStr = data.multiArray[e.detail.column][e.detail.value];
                var hour = hourStr.substring(0, hourStr.length - 1);
                data.hour = hour;

                var surplusMinute;
                if (data.year == year1 && month1 == data.month && data.day == day1 && data.hour == hour1) {
                    surplusMinute = this.surplusMinute(data.year, data.month, data.day, data.hour, data.minute)

                } else {
                    surplusMinute = this.surplusMinute(data.year, data.month, data.day, data.hour, 0)

                }
                data.multiArray[4] = surplusMinute;

                data.multiIndex[4] = 0;
                break;
            case 5:
                var minuteStr = data.multiArray[e.detail.column][e.detail.value];
                var minute = minuteStr.substring(0, minuteStr.length - 1);
                data.minute = minute;
                break;
        }
        this.setData(data)
    },
    //value 改变时触发 change 事件
    bindMultiPickerChange: function(e) {
        let {
            multiArray = [], multiIndex = []
        } = this.data
        let yearStr = multiArray[0][multiIndex[0]]
        let monthStr = multiArray[1][multiIndex[1]]
        let dayStr = multiArray[2][multiIndex[2]]
        let hourStr = multiArray[3][multiIndex[3]]
        let minuteStr = multiArray[4][multiIndex[4]]
        let dateStr = yearStr.substring(0, yearStr.length - 1) + '-' +
            monthStr.substring(0, monthStr.length - 1) + '-' +
            dayStr.substring(0, dayStr.length - 1) + ' ' +
            hourStr.substring(0, hourStr.length - 1) + ':' +
            minuteStr.substring(0, minuteStr.length - 1);
        this.setData({
            time: dateStr
        })
    },
    /**
     * 获取formId
     */
    getFormId: function (e) {
        let formId = e.detail.formId
        let time = new Date().getTime() + 7 * 24 * 60 * 60 * 1000
        if (formId != 'the formId is a mock one') {
            formIdArr.push({
                formId: formId,
                expiresTime: format.dateformat.format(new Date(time), "yyyy-MM-dd hh:mm:ss")
            })
        }
        console.log(formIdArr, 'formidarr')
        if (formIdArr.length == 10) {
            app.getFormId(formIdArr)
        }
    },

})